package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.LocationService;
import com.itheima.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class LocationServiceImpl implements LocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveUserLocation(Long userId, Double latitude, Double longitude, String addr) {

        //1. 根据用户id查询,库中是否存在我的位置
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        if(userLocation==null){
            //库中不存在位置信息，新增
            userLocation=new UserLocation();
            userLocation.setUserId(userId);
            GeoJsonPoint location = new GeoJsonPoint(longitude,latitude);
            userLocation.setLocation(location);//设置当前用户的位置
            userLocation.setAddress(addr);//位置描述
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setLastUpdated(System.currentTimeMillis());

            mongoTemplate.save(userLocation);//保存
        }else{
            //如果存在,更新记录
            userLocation.setUserId(userId);//用户id
            GeoJsonPoint location = new GeoJsonPoint( longitude,latitude);
            userLocation.setLocation(location);//设置当前用户的位置
            userLocation.setAddress(addr);//位置描述
            userLocation.setLastUpdated(userLocation.getUpdated());//上次更新时间
            userLocation.setUpdated(System.currentTimeMillis());//更新时间

            mongoTemplate.save(userLocation);//更新
        }
    }

    //搜索附近的人
    @Override
    public List<UserLocationVo> findLocation(Long userId, Long distance) {
        //1. 先根据用户id查询自己的坐标
        Query query1 = new Query(Criteria.where("userId").is(userId));
        UserLocation myLocation = mongoTemplate.findOne(query1, UserLocation.class);

        //2. 确定半径, 画个圈
        //2-1 确定半径距离
        Distance dis = new Distance(distance / 1000, Metrics.KILOMETERS);
        //2-2画个圆圈
        Circle circle = new Circle(myLocation.getLocation(), dis);

        //3 查询圆中的坐标
        //3-1 设置搜索条件
        Query query2 = new Query(
                Criteria.where("location").withinSphere(circle)
        );
        List<UserLocation> userLocationList = mongoTemplate.find(query2, UserLocation.class);

        //4 为了能够使用dubbo传递数据,我们单独封装一个Vo
        //userLocationList---userLocationVoList
        List<UserLocationVo> userLocationVoList = UserLocationVo.formatToList(userLocationList);

        return userLocationVoList;
    }
}
